package com.zlx.sharelive.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.zlx.module_base.base_api.res_data.VideoBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_fg.BaseFg;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.Constants;
import com.zlx.sharelive.R;
import com.zlx.sharelive.activity.ShortVideoDetailsAc;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.bzcoder.mediapicker.cameralibrary.util.ScreenUtils;

/**
 * 我的上传——短视频
 */
public class MyShortVideoFg extends BaseFg {

    @BindView(R.id.sw_layout)
    SwipeRefreshLayout sw_layout;
    @BindView(R.id.rv_data)
    RecyclerView rv_data;
    @BindView(R.id.tv_check_status1)
    TextView tv_check_status1;
    @BindView(R.id.tv_check_status2)
    TextView tv_check_status2;

    private String checkStatus = "0";
    private String state = "1";
    private String type1 = "2";
    private int page = 1;
    private List<VideoBean.InfoDTO.RecordsDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<VideoBean.InfoDTO.RecordsDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fg_my_short_video;
    }

    @Override
    protected void initViews() {
        super.initViews();

        initRecyclerView();
        getVideoList();

        sw_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getVideoList();
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        page = 1;
        getVideoList();
    }

    @OnClick({R.id.tv_check_status1, R.id.tv_check_status2})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_check_status1:
                checkStatus = "0";
                tv_check_status1.setTextColor(getResources().getColor(R.color.hot_pink));
                tv_check_status2.setTextColor(getResources().getColor(R.color.dove_gray));
                page = 1;
                getVideoList();
                break;
            case R.id.tv_check_status2:
                checkStatus = "1";
                tv_check_status1.setTextColor(getResources().getColor(R.color.dove_gray));
                tv_check_status2.setTextColor(getResources().getColor(R.color.hot_pink));
                page = 1;
                getVideoList();
                break;
        }
    }

    private void getVideoList() {
        ApiUtil.getHomeApi().getVideoList(SPUtil.getToken(getContext()),
                String.valueOf(page),
                type1,
                state,
                "",
                "",
                "1",
                (String) SPUtil.get(getContext(),
                        Constants.SP_ID, ""), "", "", checkStatus).observe(getViewLifecycleOwner(), new BaseObserver<>(new BaseObserverCallBack<ApiResponse<VideoBean>>() {
            @Override
            public void onSuccess(ApiResponse<VideoBean> data) {
                sw_layout.setRefreshing(false);
                mList.clear();
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                if (null != data.getData().getInfo().get(0).getRecords()) {
                                    if (data.getData().getInfo().get(0).getRecords().size() > 0) {
                                        if (checkStatus.equals("0")) {
                                            for (int i = 0; i < data.getData().getInfo().get(0).getRecords().size(); i++) {
                                                if (data.getData().getInfo().get(0).getRecords().get(i).getCheckStatus().equals("0")) {
                                                    mList.add(data.getData().getInfo().get(0).getRecords().get(i));
                                                }
                                            }
                                        } else if (checkStatus.equals("1")) {
                                            for (int i = 0; i < data.getData().getInfo().get(0).getRecords().size(); i++) {
                                                if (data.getData().getInfo().get(0).getRecords().get(i).getCheckStatus().equals("1")) {
                                                    mList.add(data.getData().getInfo().get(0).getRecords().get(i));
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));
    }


    /**
     * 初始化列表
     */
    private void initRecyclerView() {
        rv_data.setLayoutManager(new GridLayoutManager(getContext(), 2));

        mAdapter = new CommonRecyclerViewAdapter<VideoBean.InfoDTO.RecordsDTO>(getContext(), mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, VideoBean.InfoDTO.RecordsDTO entity, int position) {
                GlideImgManager.glideLoader(getContext(), entity.getVideo(), holder.getView(R.id.iv_short_video_image), 1);
                holder.setText(R.id.tv_like_count, entity.getLikeCount());
                if (entity.getIslike().equals("1")) {
                    ((ImageView) holder.getView(R.id.iv_like_count)).setImageResource(R.mipmap.fabulous_img_true);
                } else {
                    ((ImageView) holder.getView(R.id.iv_like_count)).setImageResource(R.mipmap.fabulous_img_false);
                }
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_short_video;
            }
        };

        rv_data.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if (checkStatus.equals("1")) {
                    startActivity(new Intent(getContext(),
                            ShortVideoDetailsAc.class)
                            .putExtra("id", mList.get(position).getId())
                            .putExtra("video", mList.get(position).getVideo())
                    );
                }
            }
        });
    }

    //改变bitmap尺寸的方法
    public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
        // 获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }
}
